﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using WooCoo.Tools;

namespace WooCoo.DataPortal.WebHost.ODataServer
{
    /// <summary>
    /// 定义一个controller类的属性，用于OData action route
    /// Defines a controller-level attribute that can be used to enable OData action selection based on routing conventions.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
    public sealed class ODataPortalActionAttribute : Attribute, IControllerConfiguration
    {
        /// <summary>Callback invoked to set per-controller overrides for this controllerDescriptor.</summary>
        /// <param name="controllerSettings">The controller settings to initialize.</param>
        /// <param name="controllerDescriptor">The controller descriptor. Note that the <see cref="T:System.Web.Http.Controllers.HttpControllerDescriptor" /> can be associated with the derived controller type given that <see cref="T:System.Web.Http.Controllers.IControllerConfiguration" /> is inherited.</param>
        public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
        {
            if (controllerSettings == null)
            {
                throw Error.ArgumentNull("controllerSettings");
            }
            if (controllerDescriptor == null)
            {
                throw Error.ArgumentNull("controllerDescriptor");
            }
            ServicesContainer services = controllerSettings.Services;
            // Replace the action selector with one that is based on the OData routing conventions
            IHttpActionSelector actionSelector = services.GetActionSelector();
            IHttpActionSelector service = new ODataPortalActionSelector(actionSelector);
            services.Replace(typeof(IHttpActionSelector), service);
            //services.Insert(typeof(ValueProviderFactory), 0, new ODataValueProviderFactory());
        }
    }
}
